<template>
  <div class="shareDetail">
    <van-nav-bar>
      <template #left>
        <i @click="$router.go(-1)" class="iconfont iconbtn_nav_back myicon"></i>
      </template>
    </van-nav-bar>
    <div class="main" v-if="ShareDetailObj.author">
      <!-- 文章区域 -->
      <div class="article">
        <div class="title">
          {{ ShareDetailObj.title }}
        </div>
        <div class="author">
          <img
            :src="baseUrl + ShareDetailObj.author.avatar"
            alt=""
            class="avatar"
          />
          <div>
            <div class="nickname">{{ ShareDetailObj.author.nickname }}</div>
            <div class="time">{{ ShareDetailObj.updated_at | timeFormat }}</div>
          </div>
        </div>
        <div class="content" v-html="ShareDetailObj.content"></div>
      </div>
      <!-- 评论区域 -->
      <div class="commentBox">
        <div class="commentData">
          评论 <sup>{{ total }}</sup>
        </div>
        <van-list
          v-model="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="onLoad"
        >
          <div class="comments" v-for="(item, index) in comments" :key="index">
            <div class="commentName">
              <div class="author">
                <img
                  :src="baseUrl + item.author.avatar"
                  alt=""
                  class="avatar"
                  @click="replyComment(item)"
                />
                <div>
                  <div class="nickname">{{ item.author.nickname }}</div>
                  <div class="time">{{ item.created_at | timeFormat }}</div>
                </div>
              </div>
              <div class="star">
                <span>{{ item.star ? item.star : 0 }}</span
                ><van-icon name="good-job-o" size="20" />
              </div>
            </div>
            <div class="commentsList">
              <div class="comment_content">
                {{ item.content }}
              </div>
              <div
                class="replyComment"
                v-if="item.children_comments.length != 0"
              >
                <div
                  class="replyauthor"
                  v-for="(subItem, subIndex) in item.children_comments"
                  :key="subIndex"
                >
                  <span class="name">{{ subItem.author }}：</span>
                  <p class="replyContent">{{ subItem.content }}</p>
                </div>
              </div>
            </div>
          </div>
        </van-list>
      </div>
    </div>
    <!-- 书写评论 -->
    <div class="write">
      <div class="write_left" @click="openComment">
        我来补充两句
      </div>
      <div class="write_right">
        <!-- 收藏 -->
        <div class="star" @click="goCollect">
          <van-icon name="star-o" size="20" color="#e40137" v-if="isCollect" />
          <van-icon name="star-o" size="20" v-else />
          <p>{{ ShareDetailObj.collect }}</p>
        </div>
        <div class="star" @click="goStar">
          <!-- 点赞 -->
          <van-icon name="good-job-o" size="20" color="#e40137" v-if="isStar" />
          <van-icon name="good-job-o" size="20" v-else />
          <p>{{ ShareDetailObj.star }}</p>
        </div>
        <div class="star">
          <!-- 分享 -->
          <van-icon name="share-o" size="20" />
          <p>{{ ShareDetailObj.share ? ShareDetailObj.share : 0 }}</p>
        </div>
      </div>
    </div>
    <!-- 发表评论 -->
    <van-popup
      v-model="isShow"
      @close="close"
      position="bottom"
      :style="{ height: '25%' }"
    >
      <div class="textarea">
        <van-field
          v-model="message"
          type="textarea"
          :placeholder="placeholder"
        />
      </div>
      <div class="btn">
        <van-button type="primary" size="small" @click="addComment"
          >发送</van-button
        >
      </div>
    </van-popup>
  </div>
</template>

<script>
import {
  apiGetShareDetail,
  apiGetcomments,
  apiAddComment,
  apiStar,
  apiCollect
} from '@/api/find.js'
import { mapState } from 'vuex'
// import { getLocal } from '@/utils/local.js'
export default {
  computed: {
    ...mapState({
      userInfo: state => state.userInfo
    }),
    isStar: function () {
      return (
        this.userInfo.starArticles &&
        this.userInfo.starArticles.includes(+this.id)
      )
    },
    isCollect: function () {
      return (
        this.userInfo.collectArticles &&
        this.userInfo.collectArticles.includes(+this.id)
      )
    }
  },
  data () {
    return {
      // 文章id
      id: this.$route.params.id,
      // 基地址
      baseUrl: process.env.VUE_APP_URL,
      // 文章数据
      ShareDetailObj: {},
      //   评论数据个数
      limit: 5,
      //   评论起始索引
      start: 0,
      // 评论数据
      comments: [],
      //   总评论数
      total: 0,
      loading: false,
      finished: false,
      // 发表评论框
      isShow: false,
      // 评论
      message: '',
      // 提示文本
      placeholder: '我来补充两句',
      // 是否回复
      isReply: false,
      // 评论id
      commentId: 0
    }
  },
  methods: {
    // 获取文章详情
    async getShareDetail () {
      const res = await apiGetShareDetail(this.id)
      this.ShareDetailObj = res.data

      // console.log(this.ShareDetailObj)
    },
    // 监听屏幕滚动事件
    async onLoad () {
      // 获取评论
      const res = await apiGetcomments({
        id: this.id,
        limit: this.limit,
        start: this.start
      })
      this.comments.push(...res.data.list)
      this.total = res.data.total

      // 加载状态结束
      this.loading = false
      this.start += this.limit
      if (this.start >= this.total) {
        this.finished = true
      }
    },
    // 打开评论框
    openComment () {
      this.isShow = true
      this.placeholder = '我来补充两句'
      this.isReply = false
    },
    // 发表评论
    async addComment () {
      if (this.userInfo) {
        if (this.isReply === false) {
          const res = await apiAddComment({
            article: this.id,
            content: this.message
          })
          this.comments.unshift(res.data)
          this.total += 1
          this.isShow = false
        } else {
          const res = await apiAddComment({
            parent: this.commentId,
            content: this.message
          })
          // 遍历所有评论 找到相等的评论id 追加到子评论中
          this.comments.map(item => {
            if (item.id === this.commentId) {
              item.children_comments.push(res.data)
            }
          })
          this.isShow = false
        }
      } else {
        this.$router.push('/login?redirect=' + this.$route.path)
      }
    },
    // 回复评论
    replyComment (item) {
      // 评论id
      this.commentId = item.id
      this.isShow = true
      this.isReply = true
      this.placeholder = '回复：' + item.author.nickname
    },
    // 清空评论框
    close () {
      this.message = ''
    },
    // 文章点赞
    async goStar () {
      if (this.userInfo) {
        this.$toast.loading({
          duration: 0,
          forbidClick: true
        })
        const res = await apiStar(this.id)
        this.ShareDetailObj.star = res.data.num
        // starArticles
        this.userInfo.starArticles = res.data.list
        if (this.isStar) {
          this.$toast.success('点赞成功')
        } else {
          this.$toast.success('点赞已取消')
        }
        console.log(res)
      } else {
        this.$router.push('/login?redirect=' + this.$route.path)
      }
    },
    // 文章收藏
    async goCollect () {
      if (this.userInfo) {
        this.$toast.loading({
          duration: 0,
          forbidClick: true
        })
        const res = await apiCollect(this.id)
        this.ShareDetailObj.collect = res.data.num
        this.userInfo.collectArticles = res.data.list
        console.log(res)
        if (this.isCollect) {
          this.$toast.success('收藏成功')
        } else {
          this.$toast.success('收藏已取消')
        }
      } else {
        this.$router.push('/login?redirect=' + this.$route.path)
      }
    }
  },
  created () {
    this.getShareDetail()
    this.onLoad()
  }
}
</script>

<style lang="less" scoped>
.shareDetail {
  font-size: 12px;
  /deep/.van-nav-bar {
    height: 44px;
    box-shadow: 0px 4px 8px 0px rgba(24, 26, 57, 0.04);
  }
  /deep/.van-nav-bar__left {
    padding-left: 0px;
  }
  .myicon {
    font-size: 50px;
  }
  .main {
    margin-bottom: 50px;
    .article {
      padding: 20px 15px;
      background: #ffffff;
      .title {
        font-size: 18px;
        font-weight: 500;
        color: #181a39;
        line-height: 25px;
      }

      .content {
        margin-top: 20px;
        font-size: 16px;
        line-height: 30px;
        /deep/ img {
          width: 100%;
          margin: 19px 0;
        }
      }
    }
    .commentBox {
      margin-top: 10px;
      padding: 25px 15px;
      background: #fff;
      .comments {
        .commentName {
          display: flex;
          align-items: center;
          justify-content: center;
          .star {
            display: flex;
            align-items: flex-end;
            justify-content: center;
            span {
              margin-right: 3px;
            }
          }
        }
        .commentsList {
          margin-top: 15px;
          padding-left: 42px;
          .comment_content {
            font-size: 16px;
            color: #181a39;
            word-wrap: break-word;
            word-break: break-all;
            overflow: hidden;
            line-height: 27px;
          }
          .replyComment {
            font-size: 14px;
            margin-top: 10px;
            background: #f7f4f5;
            border-radius: 8px;
            padding: 10px 7px 10px 15px;
            color: #545671;
            .replyauthor {
              display: flex;
              align-items: flex-start;
              justify-content: center;
              margin-bottom: 10px;
              &:last-child {
                margin-bottom: 0;
              }
              .name {
                color: #5e6079;
                font-weight: 600;
              }
              .replyContent {
                flex: 1;
                line-height: 20px;
                word-wrap: break-word;
                word-break: break-all;
                overflow: hidden;
              }
            }
          }
        }
      }
    }
    .author {
      display: flex;
      flex: 1;
      align-items: center;
      margin-top: 20px;
      .avatar {
        width: 35px;
        height: 35px;
        border-radius: 50%;
        margin-right: 10px;
      }
      .nickname {
        margin-bottom: 5px;
        color: #545671;

        font-weight: 600;
      }
      .time {
        color: #b4b4bd;
      }
    }
  }
  .write {
    position: fixed;
    bottom: 0;
    background: #fff;
    width: 100%;
    display: flex;
    padding: 10px 15px;
    box-sizing: border-box;
    justify-content: flex-start;
    .write_left {
      height: 34px;
      flex: 1;
      background: #f7f4f5;
      line-height: 34px;
      color: #b4b4bd;
      font-size: 14px;
      text-indent: 1em;
      border-radius: 8px;
    }
    .write_right {
      margin-left: 24px;
      flex: 1;
      display: flex;
      justify-content: space-between;
      text-align: center;
      color: #b4b4bd;
    }
  }
  .textarea {
    padding: 25px 15px 10px;
    /deep/.van-field {
      background: #f7f4f5;
      border-radius: 8px;
    }
  }
  .btn {
    padding-right: 15px;
    text-align: right;
  }
}
</style>
